package com.hnkywl.sts.action.jsgl;

import com.hnkywl.sts.dao.jsgl.XsjsdDao;
import com.hnkywl.sts.dao.jsgl.XsjsdmxDao;
import com.hnkywl.sts.entity.cggl.Jsd;
import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.service.base.DxpzService;
import com.hnkywl.sts.service.jsgl.XsjsdService;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-23
 * Time: 上午10:53
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/jsgl/xsjsd")
public class XsjsdAction extends SimpleActionSupport {
    private XsjsdService xsjsdService;
    private XsjsdDao xsjsdDao;
    private XsjsdmxDao xsjsdmxDao;
    private DxpzService dxpzService;
    private GsjgService gsjgService;

    //操作类型 add表示新增,update表示修改
    private String operation;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    //行索引
    private int index;
    //传入的企业机构Id值
    private Integer type;
    //结算单号
    private String jsdh;
    //客户的ID
    private Long glqyId;
    //发票类型ID
    private Long fpzlId;
    //客户的名称
    private String glqyQc;
    //公司的名称
    private String gsJc;
    //申请（查询）开始时间
    private Date sqrqStart;
    //申请（查询）结束时间
    private Date sqrqEnd;
    //销售结算单实体
    private Jsd xsjsd;
    //销售结算单的ID
    private Long xsjsdId;
    //申报人的Id
    private Long sprId;
    //申报人所在公司的Id
    private Long gsId;
    //状态
    private Integer zt;
    //客户ID
    private Long khId;
    //销售公司ID
    private Long xsgsId;
    //申报公司ID
    private Long sbgsId;
    //销售明细Id
    private Long xsmxId;
    //销售单id
    private Long xsdId;
    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;
    private Date CurrentTime = new Date();
    private String dateStr;
    private Integer bzjfs;
    private String htbm;
    private String xsdIds;
    private String xsmxIds;


    //销售结算单的查询分页显示

    @Action("xsjsdList")
    public void xsjsdList() {
        this.renderJson(this.xsjsdService.findAllXsjsd(zt, jsdh, sqrqStart, sqrqEnd, glqyId, glqyQc, gsId, gsJc, fpzlId, page, rows));
    }

    @Action("xsjsd")
    public String xsjsd() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    @Action("xsjsd_content")
    public String xsjsdContent() {
        sprId = YgService.getCurrentYg().getId();
        gsId = YgService.getCurrentYgZgGsId();
        //进入update页面
        if (xsjsdId != null) {
            xsjsd = xsjsdService.findXsjsdDetail(xsjsdId);
        } else {
            xsjsd = new Jsd();
            xsjsd.setGs((Nbgs) gsjgService.findGsjgDetail(gsId));
        }
        return SUCCESS;
    }

    @Action("xsjsd_detail")
    public String xsjsdDetail() {
        if (xsjsdId != null)
            xsjsd = xsjsdService.findXsjsdDetail(xsjsdId);
        return SUCCESS;
    }

    //销售结算单中表格
    @Action("xsjsd_table")
    public void xsjsdTable() {
        List<Map> fpxsjsdList;
        Jsd jsd = this.xsjsdService.findXsjsdDetail(xsjsdId);
        if (jsd.getShzt() == 1) {
            fpxsjsdList = this.xsjsdmxDao.findXsjsdmxSearch(xsjsdId);
        } else {
            fpxsjsdList = this.xsjsdDao.xsjsdList(xsjsdId);
        }
        Double sbsl = 0d;
        Double ghje = 0d;
        Double je = 0d;
        Double se = 0d;
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
        java.text.DecimalFormat jtdf = new java.text.DecimalFormat("0.00");
        java.text.DecimalFormat jtdfd = new java.text.DecimalFormat("0.000000");

        StringBuffer sbTable = new StringBuffer();
        if (null != fpxsjsdList) {
            sbTable.append("<table class=\"tb intb\">");
            sbTable.append("<tr>");
            sbTable.append("<td style='width:110px;'>销售系统子项号</td><td style='width:110px;'>销售单号</td><td width='60px'>品名</td>" +
                    " <td width='30px'>材质</td><td width='30px'>规格型号</td><td width='40px'>产地</td><td width='30px'>单位</td>" +
                    "<td style='width:70px;'>数量</td><td style='width:80px;'>含税价(元)</td><td style='width:80px;'>实际结算价(元)</td>" +
                    "<td style='width:80px;'>购货金额(元)</td><td style='width:80px;'>开票不含税单价(元)</td><td style='width:80px;'>金额(元)</td>" +
                    "<td style='width:80px;'>税额(元)</td>");
            sbTable.append("</tr>");
            for (int i = 0; i < fpxsjsdList.size(); i++) {
                sbTable.append("<tr>");

                //销售系统子项号
                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("xsxt_xsdh")) {
                    sbTable.append(fpxsjsdList.get(i).get("xsxt_xsdh"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("xsdh")) {
                    sbTable.append(fpxsjsdList.get(i).get("xsdh"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("wzgg.name")) {
                    sbTable.append(fpxsjsdList.get(i).get("wzgg.name"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("wzgg.cz")) {
                    sbTable.append(fpxsjsdList.get(i).get("wzgg.cz"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("wzgg.ggxh")) {
                    sbTable.append(fpxsjsdList.get(i).get("wzgg.ggxh"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("cd.z")) {
                    sbTable.append(fpxsjsdList.get(i).get("cd.z"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("wzgg.dw")) {
                    sbTable.append(fpxsjsdList.get(i).get("wzgg.dw"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("ck_sbsl")) {
                    sbTable.append(jtdfd.format(fpxsjsdList.get(i).get("ck_sbsl")));
                    sbsl += (Double) (fpxsjsdList.get(i).get("ck_sbsl"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("hsj")) {
                    sbTable.append(jtdf.format(fpxsjsdList.get(i).get("hsj")));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("sjdj")) {
                    sbTable.append(jtdf.format(fpxsjsdList.get(i).get("sjdj")));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("se")) {
                    sbTable.append(jtdf.format(fpxsjsdList.get(i).get("se")));
                    ghje += (Double) (fpxsjsdList.get(i).get("se"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("kpbhsj")) {
                    sbTable.append(jtdfd.format(fpxsjsdList.get(i).get("kpbhsj")));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("je")) {
                    sbTable.append(jtdf.format(fpxsjsdList.get(i).get("je")));
                    je += (Double) (fpxsjsdList.get(i).get("je"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != fpxsjsdList.get(i).get("fpse")) {
                    sbTable.append(jtdf.format(fpxsjsdList.get(i).get("fpse")));
                    se += (Double) (fpxsjsdList.get(i).get("fpse"));
                }
                sbTable.append("</td>");

                sbTable.append("</tr>");
            }
            sbTable.append("<tr>");

            sbTable.append("<th colspan=\"7\">合计：");
            sbTable.append("</th>");

            sbTable.append("<td>");
            sbTable.append(jtdf.format(sbsl));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            BigDecimal bigDecimal_total_ghje = new BigDecimal(ghje).setScale(2, BigDecimal.ROUND_HALF_UP);
            sbTable.append(jtdf.format(bigDecimal_total_ghje));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            BigDecimal bigDecimal_total_je = new BigDecimal(je).setScale(2, BigDecimal.ROUND_HALF_UP);
            sbTable.append(jtdf.format(bigDecimal_total_je));
            sbTable.append("</td>");

            sbTable.append("<td>");
            BigDecimal bigDecimal_total_se = new BigDecimal(se).setScale(2, BigDecimal.ROUND_HALF_UP);
            sbTable.append(jtdf.format(bigDecimal_total_se));
            sbTable.append("</td>");

            sbTable.append("</tr>");
            sbTable.append("</table>");
        }
        renderHtml(sbTable.toString());
    }

    /**
     * 方法名:     addXsjsd
     * 功能描述:      增加收款审核
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     */
    @Action("add_xsjsd")
    public void addXsjsd() {
        try {
            this.xsjsdService.saveXsjsd(xsjsd);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加销售结算单成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     updateXsjsd
     * 功能描述:      修改收款审核
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     */
    @Action("update_xsjsd")
    public void updateXsjsd() {
        try {
            this.xsjsdService.updateXsjsd(xsjsd);
            renderText("{\"statusCode\":\"200\",\"message\":\"修改销售结算单成功！\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }

    }

    /**
     * 方法名:     deleteXsjsd
     * 功能描述:      删除数据库收款审核
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     */
    @Action("delete_xsjsd")
    public void deleteXsjsd() {
        try {
            this.xsjsdService.deleteXsjsd(xsjsdId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除销售结算单成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //修改收款审核状态
    @Action("change_status")
    public void changeStatus() {
        try {
            xsjsdService.changeStatus(xsjsdId, zt);
//            dxpzService.sendMsg("xsjs","销售结算");
            renderText("{\"statusCode\":\"200\", \"message\":\"销售结算单已提交！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("xsd_search")
    public String xsd_search() {
        return SUCCESS;
    }

    @Action("xsd_date")
    public String xsd_date() {
        return SUCCESS;
    }

    @Action("xsjsd_txx")
    public void xsjsdTxx() {
        this.renderJson(this.xsjsdService.sumTxx(xsmxIds));
    }

    @Action("xsjsd_dlfy")
    public void xsjsdDlf() {
        Map map = this.xsjsdService.findSumDlfy(xsmxId);
        this.renderJson("{\"ztx\":\"" + map.get("TX") + "\",\"zdlf\":\"" + map.get("DLF") + "\",\"zzjzyf\":\"" + map.get("ZJZYF") + "\"}");
    }

    /**
     * 计算总贴息和代理费
     */
    @Action("xsjsd_txdlf")
    public void xsjsd_txdlf() {
        if (StringUtils.isNotBlank(xsdIds)) {
            Map map = this.xsjsdService.findSumTxDlf(xsdIds);
            this.renderJson("{\"ztx\":\"" + map.get("TX") + "\",\"zdlf\":\"" + map.get("DLF") + "\",\"zzjzyf\":\"" + map.get("ZJZYF") + "\"}");
        }
    }
    //==================get and set ================================


    public Long getXsmxId() {
        return xsmxId;
    }

    public void setXsmxId(Long xsmxId) {
        this.xsmxId = xsmxId;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public Jsd getXsjsd() {
        return xsjsd;
    }

    public void setXsjsd(Jsd xsjsd) {
        this.xsjsd = xsjsd;
    }

    public Long getXsjsdId() {
        return xsjsdId;
    }

    public Long getSprId() {
        return sprId;
    }

    public void setSprId(Long sprId) {
        this.sprId = sprId;
    }

    public Integer getZt() {
        return zt;
    }

    public void setZt(Integer zt) {
        this.zt = zt;
    }

    public void setXsjsdId(Long xsjsdId) {
        this.xsjsdId = xsjsdId;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public Long getKhId() {
        return khId;
    }

    public void setKhId(Long khId) {
        this.khId = khId;
    }

    public Long getXsgsId() {
        return xsgsId;
    }

    public void setXsgsId(Long xsgsId) {
        this.xsgsId = xsgsId;
    }

    public Long getSbgsId() {
        return sbgsId;
    }

    public void setSbgsId(Long sbgsId) {
        this.sbgsId = sbgsId;
    }

    public String getGlqyQc() {
        return glqyQc;
    }

    public void setGlqyQc(String glqyQc) {
        this.glqyQc = glqyQc;
    }

    public String getGsJc() {
        return gsJc;
    }

    public void setGsJc(String gsJc) {
        this.gsJc = gsJc;
    }

    public Date getSqrqStart() {
        return sqrqStart;
    }

    public void setSqrqStart(Date sqrqStart) {
        this.sqrqStart = sqrqStart;
    }

    public Date getSqrqEnd() {
        return sqrqEnd;
    }

    public void setSqrqEnd(Date sqrqEnd) {
        this.sqrqEnd = sqrqEnd;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getIndex() {
        return index;
    }

    public String getJsdh() {
        return jsdh;
    }

    public Long getGlqyId() {
        return glqyId;
    }

    public void setGlqyId(Long glqyId) {
        this.glqyId = glqyId;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Long getFpzlId() {
        return fpzlId;
    }

    public void setFpzlId(Long fpzlId) {
        this.fpzlId = fpzlId;
    }

    public void setJsdh(String jsdh) {
        this.jsdh = jsdh;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public Date getCurrentTime() {
        return CurrentTime;
    }

    public void setCurrentTime(Date currentTime) {
        CurrentTime = currentTime;
    }

    public String getDateStr() {
        return dateStr;
    }

    public void setDateStr(String dateStr) {
        this.dateStr = dateStr;
    }

    public Long getXsdId() {
        return xsdId;
    }

    public void setXsdId(Long xsdId) {
        this.xsdId = xsdId;
    }

    public Integer getBzjfs() {
        return bzjfs;
    }

    public void setBzjfs(Integer bzjfs) {
        this.bzjfs = bzjfs;
    }

    public String getXsdIds() {
        return xsdIds;
    }

    public void setXsdIds(String xsdIds) {
        this.xsdIds = xsdIds;
    }

    public String getHtbm() {
        return htbm;
    }

    public void setHtbm(String htbm) {
        this.htbm = htbm;
    }

    public String getXsmxIds() {
        return xsmxIds;
    }

    public void setXsmxIds(String xsmxIds) {
        this.xsmxIds = xsmxIds;
    }

    @Autowired
    public void setXsjsdService(XsjsdService xsjsdService) {
        this.xsjsdService = xsjsdService;
    }

    @Autowired
    public void setXsjsdDao(XsjsdDao xsjsdDao) {
        this.xsjsdDao = xsjsdDao;
    }

    @Autowired
    public void setXsjsdmxDao(XsjsdmxDao xsjsdmxDao) {
        this.xsjsdmxDao = xsjsdmxDao;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setDxpzService(DxpzService dxpzService) {
        this.dxpzService = dxpzService;
    }
    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }
}
